package com.hsylient.javase.chapter14.jwt;

import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;

@Slf4j
public class JWTCreateTest {

    @Test
    public void testHuToolGenerateJwt() {

        // 私钥
        String secret = "shiyaozifuchuan";


        Map<String, Object> payload = Collections.singletonMap("username", "hsylient");
        String jwtString = JWTUtil.createToken(payload, secret.getBytes());

        log.info("[JWT字符串] {}", jwtString);


        boolean verify = JWTUtil.verify(jwtString, secret.getBytes(StandardCharsets.UTF_8));
        log.info("[校验JWT] 结果 {}", verify);


        log.info("[解析JWT字符串] {}", JSON.toJSONString(JWTUtil.parseToken(jwtString), SerializerFeature.DisableCircularReferenceDetect));



        String adminJWT = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbktleSI6ImFkbWluOmp3dDphZG1pbjoxOmU0MzRmY2YyMDM5NjQ0YjM5NDU5ZDc0NCIsInNhbHQiOiIyZThhMmI2YTRiZDI0MDFkODQ2MWMzYzJfMTkyLjE2OC42Ni4xMDEiLCJsb2dpblR5cGUiOiJhZG1pbiIsInNlc3Npb25JZCI6InNoaXJvOnNlc3Npb246bWFuYWdlcjozMjY5YmVhOC0wYjUwLTQ3NDAtOWFmYi02OGNiYzIzZTlkNWUiLCJ1c2VyTmFtZSI6ImFkbWluIiwiZXhwIjoxNzIxNzAxNjY5LCJ1c2VySWQiOjF9.N6gfnnBfaG8TczCqUw3huvGzoP7WuPeQt-UbjGXw1fM";


        log.info("[解析后管Token] {}", JSON.toJSONString(JWTUtil.parseToken(adminJWT), SerializerFeature.DisableCircularReferenceDetect));
    }
}
